﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Configuration
{
    public class SystemConfiguration : ConfigurationSection
    {
        private static SystemConfiguration instance = null;
        private static readonly object mylock = new object();

        private SystemConfiguration() { }

        public static SystemConfiguration GetInstance()
        {
            lock ((mylock))
            {
                if (instance == null)
                {
                    ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
                    configFileMap.ExeConfigFilename = "Configuration/Game.config";

                    instance = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
                                    .GetSection("SystemConfiguration") as SystemConfiguration;
                }
                return instance;
            }
        }

        [ConfigurationProperty("SystemConfigurationsList")]
        public SystemConfigurationsList SystemConfigurationsList
        {
            get
            {
                return base["SystemConfigurationsList"] as SystemConfigurationsList;
            }
        }
    }

    [ConfigurationCollection(typeof(UserConfiguration), AddItemName = "UserConfiguration", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class SystemConfigurationsList : ConfigurationElementCollection
    {
        private List<UserConfiguration> _UserConfiguration;
        public List<UserConfiguration> UserConfiguration
        {
            get
            {
                if (_UserConfiguration == null)
                {
                    _UserConfiguration = new List<UserConfiguration>();
                    foreach (UserConfiguration profile in this)
                    {
                        _UserConfiguration.Add(profile);
                    }
                }
                return _UserConfiguration;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new UserConfiguration();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return element as UserConfiguration;
        }
    }

    public class UserConfiguration : ConfigurationElement
    {
        [ConfigurationProperty("ID", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string ID
        {
            get
            {
                return base["ID"] as string;
            }
            set
            {
                base["ID"] = value;
            }
        }

        [ConfigurationProperty("FullScreen", DefaultValue = false, IsKey = false, IsRequired = true)]
        public bool FullScreen
        {
            get
            {
                return Convert.ToBoolean(base["FullScreen"]);
            }
            set
            {
                base["FullScreen"] = value;
            }
        }

        [ConfigurationProperty("Resolution", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string Resolution
        {
            get
            {
                return base["Resolution"] as string;
            }
            set
            {
                base["Resolution"] = value;
            }
        }

        [ConfigurationProperty("Langue", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string Langue
        {
            get
            {
                return base["Langue"] as string;
            }
            set
            {
                base["Langue"] = value;
            }
        }

        [ConfigurationProperty("ModeDebug", DefaultValue = false, IsKey = false, IsRequired = true)]
        public bool ModeDebug
        {
            get
            {
                return Convert.ToBoolean(base["ModeDebug"]);
            }
            set
            {
                base["ModeDebug"] = value;
            }
        }
    }
}
